package com.csdemos;

import com.csdemos.algs.Deque;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DequeDemoActivity extends Activity {
	
	private Deque queue;
	
	private EditText x1;
	private EditText x2;
	private TextView array;
	private TextView result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_deque_demo);
		x1 = (EditText) findViewById(R.id.x1);
		x2 = (EditText) findViewById(R.id.x2);
		
		array = (TextView) findViewById(R.id.array);
		result = (TextView) findViewById(R.id.result);
		
		queue = new Deque();
		refresh();
	}
	
	private void setResultString(int result) {
		this.result.setText(String.valueOf(result));
	}
	
	private void refresh() {
		array.setText(queue.toString());
	}
	
	public void isEmpty(View view) {
		result.setText(String.valueOf(queue.isEmpty()));
	}
	
	public void dequeue(View view) {
		try {
			setResultString(queue.dequeue());
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}
	
	public void enqueue(View view) {
		try {
			queue.enqueue(Integer.valueOf(x1.getText().toString()));
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}
	
	public void dequeueBack(View view) {
		try {
			setResultString(queue.dequeueBack());
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}
	
	public void enqueueFront(View view) {
		try {
			queue.enqueueFront(Integer.valueOf(x2.getText().toString()));
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}

}
